//*************************************************************************************************
// ShimTest.cpp
//*************************************************************************************************
#include "stdafx.h"
#include <initguid.h>
#include <objbase.h>
#include <comdef.h>
#include <unknwn.h>
#include "..\ssapi\ssauto.h"

typedef HRESULT (_stdcall *StdApiFunc)(REFCLSID, REFIID, PVOID);
void WinError();

//*************************************************************************************************
//*************************************************************************************************
int _tmain(int argc, _TCHAR* argv[])
{
	CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

	HMODULE hMod = LoadLibrary(L"ssapi.dll");
	StdApiFunc pFunc;
	pFunc = (StdApiFunc)GetProcAddress(hMod, "DllGetClassObject");

	IClassFactory *pIClsFac;
	(*pFunc)(CLSID_VSSDatabase, IID_IClassFactory, &pIClsFac);

    HRESULT hr;
	IVSSDatabase *pIVSSDatabase;
	hr = pIClsFac->CreateInstance(NULL, IID_IVSSDatabase, (void **)&pIVSSDatabase);
	pIClsFac->Release();

//	_bstr_t ConnectionString = L"TFS:http://tkbgitvstfat01:8080";
//	_bstr_t username = L"";
//	_bstr_t password = L"";
	hr = pIVSSDatabase->Open(NULL, NULL, NULL);

	_bstr_t item = L"$/TFS4FPSE/TFSSSApiShim/ShimTest/ShimTest.cpp";
	IVSSItem *pIItem;
	hr = pIVSSDatabase->get_VSSItem(item, FALSE, &pIItem);

    BSTR itemPath;
    hr = pIItem->get_Spec(&itemPath);
    printf("Project = %ls\n", itemPath);

    long iStatus;
    hr = pIItem->get_IsCheckedOut(&iStatus);
    printf("IsCheckedOut = %s\n", iStatus == VSSFILE_NOTCHECKEDOUT ? "no" : iStatus == VSSFILE_CHECKEDOUT_ME ? "me" : "yes");

    IVSSItem *pIFolder;
    hr = pIItem->get_Parent(&pIFolder);
    BSTR pLocalSpec;
    hr = pIFolder->get_LocalSpec(&pLocalSpec);
    printf("LocalSpec = %ls\n", pLocalSpec);

    hr = pIFolder->put_LocalSpec(pLocalSpec);

    pIFolder->Release();

	pIItem->Release();
	pIVSSDatabase->Release();

	FreeLibrary(hMod);
	CoUninitialize();
	return 0;
}

//*************************************************************************************************
//*************************************************************************************************
void WinError()
{
	DWORD err = GetLastError();
	LPWSTR message;
	DWORD err2 = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
								NULL, err, 0, (LPWSTR)&message, 0, NULL);
	LocalFree(message);
}